#include "Button.h"

Button::Button(std::string buttonName, std::string path, int posX, int posY,
		Widget *parent, Listener* listener) :
		WidgetClickeableWithText(buttonName,path,posX, posY, parent) {

	this->listener = listener;
}
Button::Button(std::string buttonName, Color color, int w, int h,int posX, int posY,
		Widget *parent, Listener* listener) :
		WidgetClickeableWithText(buttonName,color,w,h,posX, posY, parent) {

	this->listener = listener;
}


void Button::handleEvent(Event event) {
	if (!isMouseOver(event))
		return;


	if ((event.getType()) == MouseButtonPressed) {
		this->pressed = true;
		Logger::log("Button", "handleEvent", "MOUSEBUTTONDOWN");
	} else if ((event.getType()) == MouseButtonReleased) {
		this->pressed = false;
		Logger::log("Button", "handleEvent", "SDL_MOUSEBUTTONUP");
	}

	WidgetClickeableWithText::handleEvent(event);
}

void Button::resolve() {
	if (this->pressed) {
		this->listener->execute();
		this->pressed = false;
	}

	WidgetClickeableWithText::resolve();
}

Button::~Button(){
	if (this->listener) delete this->listener;
}
